﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="DefaultsModule.cs" company="Anton Frattaroli">
//   Copyright (c) Anton Frattaroli. All rights reserved.
// </copyright>
// <summary>
//   Defines the DefaultsModule implementation class of the IHttpModule interface to both ensure the proper domain name and route requests with extensionless URLs to default pages.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace Lidocaine
{
    using System;
    using System.Globalization;
    using System.Web;
    
    /// <summary>
    /// Defines the DefaultsModule class.
    /// </summary>
    public class DefaultsModule : IHttpModule
    {
        #region Methods
        /// <summary>
        /// Adds event handlers to the application lifecycle.
        /// </summary>
        /// <param name="context">The HttpApplication object for the request.</param>
        public virtual void Init(HttpApplication context)
        {
            context.BeginRequest += new EventHandler(this.BeginHandler);
        }

        /// <summary>
        /// Implements the standard Dispose() method of the IHttpModule interface.
        /// </summary>
        public virtual void Dispose()
        {
        }

        /// <summary>
        /// Ensures proper domain and routes requests with extensionless URLs to default pages.
        /// </summary>
        /// <param name="sender">The HttpApplication object for the request.</param>
        /// <param name="e">The EventArgs object for the request.</param>
        private void BeginHandler(object sender, EventArgs e)
        {
            HttpApplication app = (HttpApplication)sender;
            string requestedPath = app.Request.Path;
            string requestedQuery = app.Request.Url.Query.TrimStart('?');
            string redirectPath = app.Request.Url.Scheme + "://" + app.Request.Url.Host;
            bool redirect = false;
            
            // redirect to correct domain (for example, if they get to the site without the "www." but you still need that so your cookies don't interfere with subdomains).
            if (app.Request.Url.Host.ToUpperInvariant() != Environmental.Domain.ToUpperInvariant())
            {
                redirectPath = app.Request.Url.Scheme + "://" + Environmental.Domain.ToLower(CultureInfo.CurrentCulture);
                redirect = true;
            }

            if (!requestedPath.Substring(requestedPath.LastIndexOf("/", StringComparison.Ordinal)).Contains("."))
            {
                redirectPath += requestedPath + (requestedPath.EndsWith("/", StringComparison.Ordinal) ? string.Empty : "/") + "Default." + (requestedPath == "/cms" || requestedPath == "/cms/" ? "htm" : "aspx") + (string.IsNullOrEmpty(requestedQuery) ? string.Empty : "?" + requestedQuery);
                redirect = true;
            }

            if (redirect)
            {
                app.Response.Redirect(redirectPath);
                app.CompleteRequest();
            }
        }
        #endregion
    }
}